/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 */

import { expect } from '@jest/globals';

import { initByFAJsEmptyApp, initExecuteCommand, runHvigor } from '../../common/common';
import { HOS_SIGNATURE_TEMPLATE, OHOS_SIGNATURE_TEMPLATE, Tasks } from '../constants';
import { buildTest, checkSuccessful, configJsProject } from '../utils';

describe('Hos Api8 js lite-wearable test', () => {
  test.concurrent('Test build hap package with signature', async () => {
    await initByFAJsEmptyApp(async (projectRoot) => {
      const commands = {
        mode: 'module',
        product: 'default',
        module: 'entry@default',
        tasks: Tasks.ASSEMBLE_HAP,
      };

      configJsProject(projectRoot,
        undefined,
        [{ moduleName: 'entry', deviceTypes: ['liteWearable'] }],
        {
          sdkVersion: 8,
          signConfig: HOS_SIGNATURE_TEMPLATE,
        });

      checkSuccessful(projectRoot, commands);
      buildTest(projectRoot, 'entry', 'default', Tasks.ASSEMBLE_HAP);
    }, { sign: true });
  });

  test.concurrent('Test build hap package without signature', async () => {
    await initByFAJsEmptyApp(async (projectRoot) => {

      const commands = {
        mode: 'module',
        product: 'default',
        module: 'entry@default',
        tasks: Tasks.ASSEMBLE_HAP,
      };

      configJsProject(projectRoot,
        undefined,
        [{ moduleName: 'entry', deviceTypes: ['liteWearable'] }],
        { sdkVersion: 8 });
      checkSuccessful(projectRoot, commands);
      buildTest(projectRoot, 'entry', 'default', Tasks.ASSEMBLE_HAP, { signed: false });
    }, { sign: false });
  });

  test.concurrent('Test build app package with signature', async () => {
    await initByFAJsEmptyApp(async (projectRoot) => {
      const commands = {
        mode: 'project',
        product: 'default',
        tasks: Tasks.ASSEMBLE_APP,
      };

      configJsProject(projectRoot,
        undefined,
        [{ moduleName: 'entry', deviceTypes: ['liteWearable'] }],
        {
          sdkVersion: 8,
          signConfig: HOS_SIGNATURE_TEMPLATE,
        });

      checkSuccessful(projectRoot, commands);
      buildTest(projectRoot, 'entry', 'default', Tasks.ASSEMBLE_APP);
    }, { sign: true });
  });

  test.concurrent('Test build app package without signature', async () => {
    await initByFAJsEmptyApp(async (projectRoot) => {
      const commands = {
        mode: 'project',
        product: 'default',
        tasks: Tasks.ASSEMBLE_APP,
      };

      configJsProject(projectRoot,
        undefined,
        [{ moduleName: 'entry', deviceTypes: ['liteWearable'] }],
        { sdkVersion: 8 });

      checkSuccessful(projectRoot, commands);
      buildTest(projectRoot, 'entry', 'default', Tasks.ASSEMBLE_APP, { signed: false });
    }, { sign: false });
  });

  // api8 瘦设备工程修改为api9
  test.concurrent('Test build api9 lite wearable project', async () => {
    await initByFAJsEmptyApp(async (projectRoot) => {
      const commands = initExecuteCommand({
        mode: 'module',
        product: 'default',
        module: 'entry@default',
        tasks: 'default@LegacyPreBuild',
      });

      configJsProject(projectRoot,
        undefined,
        [{ moduleName: 'entry', deviceTypes: ['liteWearable'] }],
        {
          sdkVersion: 9,
          signConfig: HOS_SIGNATURE_TEMPLATE,
        });

      const result = runHvigor(projectRoot, commands);
      expect(result.stderr.includes('Packaging API9 & Lite-Device project is not supported.')).toBeTruthy();
    });
  });

  // 不能构建API8 liteWearable应用的测试包
  test.concurrent('Test build test module of api8 liteWearable project', async () => {
    await initByFAJsEmptyApp(async (projectRoot) => {
      const commands = initExecuteCommand({
        mode: 'module',
        module: 'entry@ohosTest',
        tasks: 'ohosTest@LegacyPreBuild',
      });

      configJsProject(projectRoot,
        undefined,
        [{ moduleName: 'entry', deviceTypes: ['liteWearable'] }],
        {
          sdkVersion: 8,
          signConfig: HOS_SIGNATURE_TEMPLATE,
        });

      const result = runHvigor(projectRoot, commands);

      expect(result.stderr.includes('The API8 & Lite-Device project does not support compiling test package.'))
        .toBeTruthy();
    });
  });

  // 改造工程为ohos Api8 liteWearable工程
  test.concurrent('Test build ohos api8 liteWearable project', async () => {
    await initByFAJsEmptyApp(async (projectRoot) => {
      const commands = {
        mode: 'module',
        product: 'default',
        module: 'entry@default',
        tasks: Tasks.ASSEMBLE_HAP,
      };

      configJsProject(projectRoot,
        [{
          moduleName: 'entry',
          updateTarget: [{ name: 'default', runtimeOS: 'OpenHarmony' }],
        }],
        [{ moduleName: 'entry', deviceTypes: ['liteWearable'] }],
        {
          sdkVersion: 8,
          signConfig: OHOS_SIGNATURE_TEMPLATE,
          updateProducts: [
            { name: 'default', bundleName: 'com.example.myapplication' },
          ],
        });

      checkSuccessful(projectRoot, commands);
      buildTest(projectRoot, 'entry', 'default', Tasks.ASSEMBLE_HAP);
    });
  });

  // 修改工程为hos Api8 的其他瘦设备工程
  test.concurrent('Test build others hos api8 lite-device project', async () => {
    await initByFAJsEmptyApp(async (projectRoot) => {
      const commands = {
        mode: 'module',
        product: 'default',
        module: 'entry@default',
        tasks: Tasks.ASSEMBLE_HAP,
      };

      configJsProject(projectRoot,
        undefined,
        [{ moduleName: 'entry', deviceTypes: ['smartVision'] }],
        {
          sdkVersion: 8,
          signConfig: HOS_SIGNATURE_TEMPLATE,
        });

      checkSuccessful(projectRoot, commands);
      buildTest(projectRoot, 'entry', 'default', Tasks.ASSEMBLE_HAP);
    });
  });
});
